<?php
namespace App\Controller;

class VideoTopicsController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
        $this->loadModel('Videotopics');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取指定的视频主题
   * */
    public function getInfo()
    {
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $videotopics = $this->Videotopics->findById($id)->first();
        if(empty($videotopics)){
            $this->G->error("videotopics_error");
            return null;
        }
        $this->loadComponent('CategoryContentCache');
        $cate_id = $this->CategoryContentCache->getCategories('videotopics',$videotopics['id']);
        $videotopics['category_id'] = empty($cate_id) ? 0 : $cate_id[0];
        $videotopics['add_time'] = date('Y-m-d H:i:s', $videotopics['add_time']);
        $this->G->success("Success", $videotopics);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取视频主题所属分类
   * */
    public function getCategories()
    {
        $this->loadComponent('SysconfigCache');
        $category_id = $this->SysconfigCache->getVal('video_category');
        $this->loadComponent('CategoryCache');
        $cache = $this->CategoryCache->getVal($category_id);
        $data = [];
        if(!empty($cache)){
            foreach ($cache as $k=>$v){
                if($v['id'] == $category_id){
                    $data = $v;
                }
            }
        }
        $this->G->success("Success", $data);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：删除视频主题
   * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData("ids");
        if(empty($ids) || !is_array($ids)){
            $this->G->error("ids_error");
            return null;
        }
        $this->loadComponent('VideoTopicCache');
        $this->loadComponent('CategoryContentCache');
        $this->loadModel('CategoryContents');
        foreach ($ids as $k=>$v){
            $this->CategoryContentCache->deleteCategories('videotopics', $v);
            $this->VideoTopicCache->deleteCache($v);
        }
        $this->VideoTopicCache->deleteList();
        $this->Videotopics->deleteAll(['id in'=>$ids]);
        $this->CategoryContents->deleteAll(['content_id in'=>$ids,'content_type'=>'videotopics']);
        $this->G->success("Success");
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.18
   * ps：修改视频主题
   * */
    public function update()
    {
        $id = $this->request->getData("id");
        $category_id = $this->request->getData("category_id");
        $title = trim($this->request->getData("title"));
        $intro = $this->request->getData("intro");
        $img = $this->request->getData("img");
        $current = $this->request->getData("current");
        $total = $this->request->getData("total");
        $author = $this->request->getData("author");
        $order_num = $this->request->getData("order_num");
        $enabled = $this->request->getData("enabled");
        $open = $this->request->getData("open");
        $open_time = $this->request->getData("open_time");
        if(!is_numeric($category_id)){
            $this->G->error("category_id_error");
            return null;
        }
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(empty($title)){
            $this->G->error("title_error");
            return null;
        }
        if(empty($img)){
            $this->G->error("img_error");
            return null;
        }
        if(empty($open_time)){
            $this->G->error("open_time_error");
            return null;
        }
        $videotopics = $this->Videotopics->findById($id)->select(['id'])->first();
        if(empty($videotopics)){
            $this->G->error('videotopic_not_found');
            return null;
        }
        $order_num = empty($order_num) ? 0 : $order_num;
        $videotopic = $this->Videotopics->newEntity();
        $videotopic->id = $id;
        $videotopic->open_time = $open_time;
        $videotopic->title = $title;
        $videotopic->intro = $intro;
        $videotopic->img = $img;
        $videotopic->current = $current;
        $videotopic->total = $total;
        $videotopic->author = $author;
        $videotopic->order_num = $order_num;
        $videotopic->enabled = $enabled;
        $videotopic->open = $open;
        $this->Videotopics->save($videotopic);
        $this->loadModel('CategoryContents');
        $category_contents = $this->CategoryContents->find()
            ->where(['content_id'=>$id,'content_type'=>'videotopics'])
            ->select(['id'])
            ->first();
        if(!empty($category_contents)){
            $this->CategoryContents->query()
                ->update()
                ->set(['category_id'=>$category_id])
                ->where(['id' => $category_contents['id']])
                ->execute();
        }else{
            $this->loadModel('CategoryContents');
            $category_contents = $this->CategoryContents->newEntity();
            $category_contents->category_id = $category_id;
            $category_contents->content_type = 'videotopics';
            $category_contents->content_id = $id;
            $this->CategoryContents->save($category_contents);
        }
        $this->loadComponent('CategoryContentCache');
        $this->CategoryContentCache->deleteCategories('videotopics', $id);
        $this->loadComponent('VideoTopicCache');
        $this->VideoTopicCache->deleteCache($id);
        $this->VideoTopicCache->deleteList();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.18
   * ps：新增视频主题
   * */
    public function add()
    {
        $category_id = $this->request->getData("category_id");
        $title = trim($this->request->getData("title"));
        $intro = $this->request->getData("intro");
        $img = $this->request->getData("img");
        $current = $this->request->getData("current");
        $total = $this->request->getData("total");
        $author = $this->request->getData("author");
        $order_num = $this->request->getData("order_num");
        $enabled = $this->request->getData("enabled");
        $open = $this->request->getData("open");
        $open_time = $this->request->getData("open_time");
        if(empty($title)){
            $this->G->error("title_error");
            return null;
        }
        if(empty($img)){
            $this->G->error("img_error");
            return null;
        }
        if(empty($open_time)){
            $this->G->error("open_time_error");
            return null;
        }
        $order_num = empty($order_num) ? 0 : $order_num;
        $videotopic = $this->Videotopics->newEntity();
        $videotopic->title = $title;
        $videotopic->intro = $intro;
        $videotopic->img = $img;
        $videotopic->current = $current;
        $videotopic->total = $total;
        $videotopic->author = $author;
        $videotopic->order_num = $order_num;
        $videotopic->enabled = $enabled;
        $videotopic->open = $open;
        $videotopic->open_time = $open_time;
        $videotopic->add_time = time();
        $videotopic_save = $this->Videotopics->save($videotopic);
        if(!empty($videotopic_save)){
            $this->loadModel('CategoryContents');
            $category_contents = $this->CategoryContents->newEntity();
            $category_contents->category_id = $category_id;
            $category_contents->content_type = 'videotopics';
            $category_contents->content_id = $videotopic_save->id;
            $this->CategoryContents->save($category_contents);
        }
        $this->loadComponent('VideoTopicCache');
        $this->VideoTopicCache->deleteList();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.18
   * ps：获取视频主题列表
   * */
    public function getList()
    {
        $data = $this->request->getData();
        $this->loadComponent('VideoTopicCache');
        if(!empty($data)){
            ksort($data, SORT_NATURAL);
        }
        $key = md5(json_encode($data));
        $data = $this->VideoTopicCache->getList($data);
        $list = ['pageTotal'=>0,'list'=>[]];
        if(empty($data[$key]['list'])){
            $this->G->success('Success', $list);
            return null;
        }
        $list['pageTotal'] = floor($data[$key]['total']/$this->_page_num);
        foreach ($data[$key]['list'] as $k=>$v){
            $videotopic = $this->VideoTopicCache->getVal($v);
            if(empty($videotopic)){
                continue;
            }
            $videotopic['img'] = empty($videotopic['img']) ? '' : $this->G->getImgUrl($videotopic['img']);
            $list['list'][] = $videotopic;
        }
        $this->G->success('Success', $list);
    }

}
?>